﻿using AiMoWindows.CodeScripts;
using AiMoWindows.CodeScripts.IO;
using AiMoWindows.Localization;

namespace AiMoWindows.MainActions.SecondaryActions
{
    class ScriptDownloadResult
    {
        public string ShareKey { get; set; } = "";
        public bool Result { get; set; } = false;
        public string SyncToScriptInfo { get; private set; } = "";
        public string RhinoVersion { get; set; } = "";
        public byte[] Bytes { get; set; } = Array.Empty<byte>();

        public bool SyncToScriptSource(ScriptSource source)
        {
            if (!CodeIO.FromBytes(Bytes, source, out string error))
            {
                SyncToScriptInfo = error;
                return false;
            }
            source.Attributes.SetSourceFromShareKey(ShareKey);
            if (!string.IsNullOrEmpty(RhinoVersion) && RhinoVersion != Server.ServerHelper.RhinoVersion)
            {
                if (!string.IsNullOrEmpty(SyncToScriptInfo)) SyncToScriptInfo += "\r\n";
                SyncToScriptInfo = string.Format(Local.Get("Downloaded code runinng in AiMoRhino version {0}, is not same with current AiMoRhino version {1}. This may cause problems."), RhinoVersion, Server.ServerHelper.RhinoVersion); ;
            }
            return true;
        }
    }
}
